package com.jp.knowledge.my.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * @author gzejia 978862664@qq.com
 */
public class MyUtil {

    private MyUtil() {
        /* cannot be instantiated*/
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 启动软键盘
     *
     * @param context  Context
     * @param editText 关联文本编辑框
     */
    public static void startKeyword(Context context, EditText editText) {
        editText.requestFocus();
        InputMethodManager imm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 关闭软键盘
     *
     * @param context  Context
     * @param editText 关联文本编辑框
     */
    public static void closeKeyword(Context context, EditText editText) {
        editText.clearFocus();
        InputMethodManager imm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    /**
     * 关闭软键盘
     *
     * @param activity Activity
     */
    public static void closeKeyword(Activity activity) {
        if (null != activity.getCurrentFocus()) {
            ((InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 切换软键盘状态
     *
     * @param context Context
     */
    public static void changeKeyword(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 设置指定位置文字的颜色，大小倍数（0:原始大小）
     */
    public static SpannableStringBuilder changeTextType(String str, int fontSize, int colorRes,
                                                        int startIndex, int endIndex) {
        if (TextUtils.isEmpty(str)) return null;
        ColorStateList color = ColorStateList.valueOf(colorRes);
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new TextAppearanceSpan(null, 0, fontSize, color, null), startIndex, endIndex,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        return style;
    }

    /**
     * 设置指定位置文字的颜色
     */
    public static SpannableStringBuilder changeTextColor(String str, int colorRes,
                                                         int startIndex, int endIndex) {
        return changeTextType(str, 0, colorRes, startIndex, endIndex);
    }
}
